package com.link.base.base.codetemplate.controller;

import com.link.base.base.codetemplate.model.CodeTemplate;
import com.link.base.base.codetemplate.model.CodeTemplateLog;
import com.link.base.base.codetemplate.service.CodeTemplateLogService;
import com.link.base.base.codetemplate.service.CodeTemplateService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.modules.BatchExportFileWriter;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  姚润发
 * @date:   2018-08-20
 * @version 1.0
 */

@Controller
@RequestMapping("/link/codeTemplate")
public class CodeTemplateController extends BasicController<CodeTemplate> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private CodeTemplateService codeTemplateService;

    @Resource
    private CodeTemplateLogService codeTemplateLogService;

    @Override
    public BasicService<CodeTemplate> getBasicService() throws Exception {
        return codeTemplateService;
    }

    /**
     * 根据前端生成的代码信息，在后台生成压缩文件返回
     *
     * @param templatesList [{jsName:..,jsCode:..,..},{}]
     * @return 是否成功信息，成功则还有压缩文件url
     * @author yrf
     */
    @RequestMapping("genCodeFile")
    @ResponseBody
    public Map<String, Object> genCodeFile(@RequestBody List<Map<String, String>> templatesList, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Map<String, String> templateResultMap = new HashMap<>(16);
            String templateIds = "";
            //使用的模板
            String templateDisplayNames = "";
            //自动配置id
            Long autoConfId = Long.parseLong(request.getParameter("autoConfId"));
            if (templatesList != null) {
                for (Map<String, String> templateMap : templatesList) {
                    String folderName = templateMap.get("folderName");
                    //文件夹名称,后台写文件时再换回来，别问我为什么。。。
                    folderName = folderName == null ? "" : folderName.replace("/", ".");
                    // js代码
                    String jsName = templateMap.get("jsName");
                    if (!StringUtils.isBlank(jsName)) {
                        jsName = jsName.replace(".", "#");
                        templateResultMap.put(folderName + ".js." + jsName, templateMap.get("jsCode"));
                    }
                    // html代码
                    String htmlName = templateMap.get("htmlName");
                    if (!StringUtils.isBlank(htmlName)) {
                        htmlName = htmlName.replace(".", "#");
                        templateResultMap.put(folderName + "." + htmlName, templateMap.get("htmlCode"));
                    }
                    //vue 组件代码
                    String vueName = templateMap.get("vueName");
                    if (!StringUtils.isBlank(vueName)) {
                        vueName = vueName.replace(".", "#");
                        templateResultMap.put(folderName + "." + vueName, templateMap.get("vueCode"));
                    }
                    templateIds += templateMap.get("templateId") + ",";
                    templateDisplayNames += templateMap.get("templateDisplayName") + ",";
                }

                CodeTemplateLog codeTemplateLog = new CodeTemplateLog();
                codeTemplateLog.setAutoConfId(autoConfId);
                codeTemplateLog.setTempalateIds(templateIds);
                codeTemplateLog.setTempalateName(templateDisplayNames);
                codeTemplateLog.setId(keyGenerateService.keyGenerate());
                //插入数据到日志表，记录有谁使用了本接口
                codeTemplateLogService.insert(codeTemplateLog);

                BatchExportFileWriter writer = new BatchExportFileWriter();
                //生成目录文件及压缩包
                writer.writeBatchText(templateResultMap);
                result.put("url", writer.getUrlZipPath());
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("result", "参数不能为空");
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
